package fourfaith.com.ff_sl21_app.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import java.util.HashMap;
import java.util.Map;

import fourfaith.com.ff_sl21_app.utils.version.AppVersionInfo;
import fourfaith.com.ff_sl21_app.utils.version.UpdateService;

/**
 * Created by Admin on 2016/3/2.
 */
public class CheckAppVer {

    private CheckAppVer(){}

    private static CheckAppVer instance = new CheckAppVer();
    private Dialog dialog;

/*    private boolean isFirst = true;*/



    public static CheckAppVer getInstance(){
        return instance;
    }

    public void checkVersion(final Context context){
     /*   if (isFirst){*/
    /*        new Thread(new Runnable() {
                @Override
                public void run() {*/
                   // isFirst = false;
                    dialog = new LoadingDialog(context).setMessage("正在检测版本...");
                    dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();

                    Map<String, String> params = new HashMap<>();
                    RequestQueue mQueue = RequestQueueSingleton.getInstance(context).getRequestQueue();
                    JsonRequest<ResponseResult> jsonRequest = new JsonRequest<>(Request.Method.GET, Constants.versionInfo, ResponseResult.class,
                            new Response.Listener<ResponseResult>() {
                                @Override
                                public void onResponse(ResponseResult responseResult) {
                                    JSONObject dataObj = (JSONObject) responseResult.getData();
                                    AppVersionInfo appVerInfo = new AppVersionInfo();
                                    appVerInfo.setVerCode(dataObj.getString("verCode"));
                                    appVerInfo.setVerName(APKVersionCodeUtils.getVerName(context));
                                    appVerInfo.setDownUrl(dataObj.getString("downUrl"));
                                    if (appVerInfo != null && appVerInfo.getVerCode() != null && !appVerInfo.getVerCode().equals(String.valueOf(APKVersionCodeUtils.getVersionCode(context)))) {
                                        showUpdateDialog(appVerInfo, context);
                                    }else{
                                        Toast.makeText(context, "当前是最新版本", Toast.LENGTH_SHORT).show();
                                    }
                                    if (dialog.isShowing() && dialog != null) {
                                        dialog.dismiss();
                                    }
                                }

                            }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }

                    },params);
                    mQueue.add(jsonRequest);

        /*          }
          }).start();*/
       /* }*/
    }

    public void showUpdateDialog(final AppVersionInfo appVerInfo, final Context context) {
        String str = "发现新版本：" + appVerInfo.getVerName() + " ,是否更新？";
        Dialog dialog = new AlertDialog.Builder(context).setTitle("软件更新").setMessage(str)
                .setNegativeButton("暂不更新",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int whichButton) {
                                // 点击"取消"按钮之后退出程序
                                dialog.dismiss();
                            }
                        })
                .setPositiveButton("更新",// 设置确定按钮
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {

//                                new AlertDialog.Builder(context).setTitle("软件更新中...").setMessage("下载进度").create().show();
//								m_progressDlg.setTitle("正在下载");
//								m_progressDlg.setMessage("请稍候...");
//								downFile(Common.UPDATESOFTADDRESS);  //开始下载
//                                downFile(appVerInfo.getDownUrl(), context);
                                Intent downloadIntent = new Intent(context, UpdateService.class);
                                downloadIntent.putExtra("downUrl", appVerInfo.getDownUrl());
                                context.startService(downloadIntent);
                            }
                        })
                .create();// 创建
        // 显示对话框
        dialog.show();
    }

}
